
"""
6.N字形变换
https://leetcode.cn/problems/longest-substring-without-repeating-characters/

输入：s = "PAYPALISHIRING", numRows = 4
输出："PINALSIGYAHRPI"
解释：
P     I    N
A   L S  I G
Y A   H R
P     I
"""


class Solution:
    def convert(self, s: str, numRows: int) -> str:
        if numRows <= 1:
            return s

        lst = [""]*numRows  # 有几行，就需要几个临时字符串

        i = 0  # 标记行
        flag = -1  # 用来记录行的走动，1正着走，-1倒着走
        for char in s:
            lst[i] += char

            # 最重要的就是这个flag，用来标记行的来回走动
            if i == 0 or i == numRows - 1:
                flag = -flag

            i += flag

        return "".join(lst)


def test():
    obj = Solution()
    res = obj.convert("PAYPALISHIRING", 4)
    print(res)


if __name__ == "__main__":
    test()
